package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.cache.BlacklistInfoCache;
import cn.juque.jdfsystem.entity.BlacklistInfo;
import cn.juque.jdfsystem.service.IBlacklistInfoService;
import cn.juque.lucenecandy.core.base.CountQueryWrapperBuilder;
import cn.juque.lucenecandy.core.base.DeleteByIdsWrapperBuilder;
import cn.juque.lucenecandy.core.base.IdQueryWrapperBuilder;
import cn.juque.lucenecandy.core.base.QueryWrapperBuilder;
import cn.juque.lucenecandy.core.enums.MatchTypeEnum;
import cn.juque.lucenecandy.helper.IndexHelper;
import org.apache.lucene.search.BooleanClause;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>BlacklistInfoServiceImpl</li>
 * </ul>
 * @date 2023-12-19 09:41:09
 **/
@Service("BlacklistInfoService")
public class BlacklistInfoServiceImpl implements IBlacklistInfoService {

    @Resource
    private IndexHelper indexHelper;

    @Resource
    private BlacklistInfoCache blacklistInfoCache;

    /**
     * 分页查询
     *
     * @param pageInfo      pageInfo
     * @param blacklistInfo blacklistInfo
     * @return list
     */
    @Override
    public List<BlacklistInfo> listInfoWithPage(PageInfo pageInfo, BlacklistInfo blacklistInfo) {
        QueryWrapperBuilder<BlacklistInfo> builder = this.buildQuery(blacklistInfo);
        builder.pageInfo(pageInfo);
        return this.indexHelper.searchByPage(builder.build());
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return ConfigInfo
     */
    @Override
    public BlacklistInfo detail(String id) {
        IdQueryWrapperBuilder<BlacklistInfo> idQueryWrapperBuilder = new IdQueryWrapperBuilder<>(BlacklistInfo.class, id);
        return this.indexHelper.searchById(idQueryWrapperBuilder.build());
    }

    /**
     * 保存
     *
     * @param blacklistInfo blacklistInfo
     */
    @Override
    public void saveInfo(BlacklistInfo blacklistInfo) {
        CountQueryWrapperBuilder<BlacklistInfo> countQueryWrapperBuilder = new CountQueryWrapperBuilder<>(BlacklistInfo.class);
        countQueryWrapperBuilder.matchStr(BlacklistInfo::getIp, blacklistInfo.getIp(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        long count = this.indexHelper.count(countQueryWrapperBuilder.build());
        if (count > 0) {
            throw new AppException(MessageEnum.PARAM_ERROR.getCode(), "该IP已在黑名单列表");
        }
        this.indexHelper.addDocument(blacklistInfo);
        this.blacklistInfoCache.refresh();
    }

    /**
     * 更新
     *
     * @param blacklistInfo blacklistInfo
     */
    @Override
    public void updateInfo(BlacklistInfo blacklistInfo) {
        QueryWrapperBuilder<BlacklistInfo> queryWrapperBuilder = new QueryWrapperBuilder<>(BlacklistInfo.class);
        queryWrapperBuilder.matchStr(BlacklistInfo::getIp, blacklistInfo.getIp(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        List<BlacklistInfo> list = this.indexHelper.search(queryWrapperBuilder.build());
        if (CollUtil.isNotEmpty(list)) {
            long count = list.stream().filter(f -> !f.getId().equals(blacklistInfo.getId())).count();
            if (count > 0) {
                throw new AppException(MessageEnum.PARAM_ERROR.getCode(), "该IP已在黑名单列表");
            }
        }
        this.indexHelper.updateDocumentById(blacklistInfo);
        this.blacklistInfoCache.refresh();
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            DeleteByIdsWrapperBuilder<BlacklistInfo> deleteByIdsWrapperBuilder =
                    new DeleteByIdsWrapperBuilder<>(BlacklistInfo.class, ids);
            this.indexHelper.deleteByIds(deleteByIdsWrapperBuilder.build());
        }
        this.blacklistInfoCache.refresh();
    }

    private QueryWrapperBuilder<BlacklistInfo> buildQuery(BlacklistInfo blacklistInfo) {
        QueryWrapperBuilder<BlacklistInfo> builder = new QueryWrapperBuilder<>(BlacklistInfo.class);
        if (CharSequenceUtil.isNotEmpty(blacklistInfo.getIp())) {
            builder.matchStr(BlacklistInfo::getIp, blacklistInfo.getIp(), MatchTypeEnum.PREFIX, BooleanClause.Occur.MUST);
        }
        if (CharSequenceUtil.isNotEmpty(blacklistInfo.getRemark())) {
            builder.matchStr(BlacklistInfo::getIp, blacklistInfo.getRemark(), MatchTypeEnum.ALL_LIKE, BooleanClause.Occur.MUST);
        }
        return builder;
    }
}
